<html>
<head>
	<title>Period Graph Result</title>
<link rel="stylesheet" type="text/css" href="./styles1/tabber.css">
<script type="text/javascript" src="./js/tabber.js"></script>

<?php
    include 'header.php';
  ?>
<body>

<?php
$debug=0;
$phylum=$_GET['arg1'];
//echo "Phylum is: $phylum<br>";
$period=$_GET['arg2'];
//echo "Period is: $period<br>";

$username = "fossilplot";
$password = "fossilplot";
$hostname = "localhost"; 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
 or die("Unable to connect to MySQL");
//select a database to work with
$selected = mysql_select_db("fossilplot",$dbhandle)
  or die("Could not select examples");

$pd_short=mysql_query("select pd_short from period where pd='$period'") or die ("error selecting from stgintv!!");
while($row_pd_short=mysql_fetch_array($pd_short)){
    $short=$row_pd_short{'pd_short'};
    //echo "short is ".$row_pd_short{'pd_short'}."<br>";

}


if($short=="Pg"){
    $genera=mysql_query("select class_name, class_id, count(class_id) from genus where phylum_name='$phylum' and ((fad_period='T' and fad_date >='23.03') or (lad_period='T' and lad_date >='23.03')) group by class_id") or die ("error selecting from period!!");
}
else if($short =="Ng"){
        $genera=mysql_query("select class_name, class_id, count(class_id) from genus where phylum_name='$phylum' and ((fad_period='T' and fad_date <'23.03') or (lad_period='T' and lad_date <'23.03')) group by class_id") or die ("error selecting from period!!");

}
else if($short == "VC"){
        //echo "it is true!";
        $genera=mysql_query("select class_name, class_id, count(class_id) from genus where phylum_name='$phylum' and (fad_period='V' or fad_period ='Cm' or lad_period='V' or lad_period ='Cm') group by class_id") or die ("error selecting from period!!");

}

else{
 $genera=mysql_query("select class_name, class_id, count(class_id) from genus where (fad_period='$short' or lad_period='$short') and phylum_name='$phylum' group by class_id") or die ("error selecting from stgintv!!");
}

$x_arr=array();
$y_arr=array();

$i=0;
while ($row = mysql_fetch_array($genera)) {
if($row{'class_name'}){
  $tmp=$row{'class_name'};
  if((strpos($tmp,"\""))>=0){ //some class names contains "\"", need to delete them, otherwise inthe xaxis array it will has error like $datax=(..., ""CYSTOIDEA", Cl. DIPLOPORITA", ...)
    //echo "truetrue!";
    $x_arr[$i]=str_replace("\"","",$tmp);
  }
  else{
    $x_arr[$i]= $row{'class_name'};
  }
}
else{
$x_arr[$i]="Blank Class";
}
$y_arr[$i]= $row{'count(class_id)'};
$i++;
}

if($debug){
for($i=0; $i<sizeof($x_arr);$i++){
  echo "x axis is $x_arr[$i];y axis is $y_arr[$i]<br>";
}
echo "size x".sizeof($x_arr)."<br>";
echo "size y".sizeof($y_arr)."<br>";
}
$File = "../../../graph/classx.php"; 
$Handle = fopen($File, 'w');
$Data = "<?php
include (\"./src/jpgraph.php\");
include (\"./src/jpgraph_bar.php\");
//\$datax=\$x_arr;
//\$datay=\$y_arr;
\$datax=array";
fwrite($Handle, $Data);
$barx="(\"$x_arr[0]\"";
for($i=1; $i<sizeof($x_arr); $i++){
  $barx =$barx.","."\"".$x_arr[$i]."\"";
}
$barx=$barx.")";
$Data="$barx;

\$datay=array";
fwrite($Handle, $Data);
$bary="(\"$y_arr[0]\"";
for($i=1; $i<sizeof($y_arr); $i++){
  $bary =$bary.","."\"".$y_arr[$i]."\"";
}
$bary=$bary.")";
$Data="$bary;

\$graph = new Graph(800,450,\"auto\");    
\$graph->SetScale(\"textlin\");
\$graph->SetShadow();
\$graph->img->SetMargin(40,30,20,140);
\$bplot = new BarPlot(\$datay);

\$bplot->SetFillColor('orange');
\$bplot->value->Show();
\$graph->Add(\$bplot);

\$graph->title->Set(\"Class - # of Genera bar graph in $phylum in $period period\");
\$graph->xaxis->title->Set(\"Class Names\");
\$graph->yaxis->title->Set(\"Number of Genera\");
\$graph->xaxis->SetTickLabels(\$datax);
\$graph->xaxis->SetLabelAngle(45);
\$graph->xaxis->title->SetMargin(90);
\$graph->title->SetFont(FF_FONT1,FS_BOLD);
\$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
\$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
\$graph->xaxis->SetFont(FF_ARIAL,FS_NORMAL,6);
\$graph->Stroke();
?>";

fwrite($Handle, $Data);


$myFile = "../../../graph/jpgraph_cache/classx.png";
    if(file_exists($myFile)){
    unlink($myFile);
   }
echo "<img src=\"http://www.fossilplot.org/test/graph/classx.php\">";



?>
<?php include 'footer.php';?>

</body>
</html>